//
//  ByShopProductDeatilVC.m
//  hutouben_ios
//
//  Created by pro on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByShopProductDeatilVC.h"

// vc
#import "ByShopProductDetailSkuVC.h"    // 规格选择

// view
#import "ByCoreViewCarousel.h"          // 轮播视图

// cell
#import "ByShopDetailProductInfoCell.h" // 信息cell
#import "ByShopDetailProductDescCell.h" // 描述cell

//
#import "SkuModel.h"
#import "SkuIdModel.h"
#import "SkuNameModel.h"


@interface ByShopProductDeatilVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) ByCoreViewCarousel        *carousel;     // 轮播
@property (nonatomic, strong) UITableView               *table;        // 表视图
@property (nonatomic, strong) UIControl                 *skuView;      // 规格选择视图
@property (nonatomic, strong) UIView                    *toolBar;      // 工具栏

// model
@property (nonatomic, strong) ByNetProductDetailModel   *model;        // model


@end

@implementation ByShopProductDeatilVC

#pragma mark - 懒加载
- (ByCoreViewCarousel *)carousel {
    
    if (!_carousel) {
        _carousel = [[ByCoreViewCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH) timerDuration:5];
        _carousel.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    }
    return _carousel;
}
- (ByNetProductDetailModel *)model {
    
    if (!_model) {
        _model = [[ByNetProductDetailModel alloc] init];
        _model.title = @"日前，加拿大流行歌手贾斯汀·比伯现身伦敦，参加某电影的试映会";
        _model.attr = @"金色 土豪";
        _model.price = @100;
        _model.desc = @"1994年出生的比伯很小就有表演天赋，从五岁起他就无师自通，自学了钢琴、打鼓、吉他和小号，比伯还非常热爱唱歌。比伯在12岁时，获得了斯特拉当地歌唱比赛第3名，后来凭借一首《baby》红遍全球。";
        _model.postage = @14;
        _model.params = @[@"", @"", @""];
        
        SkuNameModel *nameModel1 = [[SkuNameModel alloc] init];
        nameModel1.sid_des = @"颜色";
        nameModel1.vidAry_des = @[@"红黑色", @"黄绿色"];
        SkuNameModel *nameModel2 = [[SkuNameModel alloc] init];
        nameModel2.sid_des = @"尺寸";
        nameModel2.vidAry_des = @[@"炒鸡大", @"非常小"];

        _model.skuNameAry = @[nameModel1, nameModel2];
        
        SkuIdModel *idModel1 = [[SkuIdModel alloc] init];
        idModel1.sid = @"10";
        idModel1.vidAry = @[@"11", @"12"];
        SkuIdModel *idModel2 = [[SkuIdModel alloc] init];
        idModel2.sid = @"20";
        idModel2.vidAry = @[@"21", @"22"];
        
        _model.skuIdAry = @[idModel1, idModel2];
        
        SkuModel *model1 = [[SkuModel alloc] init];
        model1.sku_id = @"10:11;20:21";
        model1.quantity = @"1";
        model1.sku_desc = @"红黑色;炒鸡大";
        SkuModel *model2 = [[SkuModel alloc] init];
        model2.sku_id = @"10:11;20:22";
        model2.quantity = @"1";
        model2.sku_desc = @"黄绿色;非常小";
        SkuModel *model3 = [[SkuModel alloc] init];
        model3.sku_id = @"10:12;20:21";
        model3.quantity = @"1";
        model3.sku_desc = @"红黑色;炒鸡大";
        SkuModel *model4 = [[SkuModel alloc] init];
        model4.sku_id = @"10:12;20:22";
        model4.quantity = @"0";
        model4.sku_desc = @"黄绿色;非常小";
        _model.skuAry = @[model1, model2, model3, model4];

    }
    return _model;
}

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商品名称";
    
    [self addLeftBackBarButtonItem];

    [self createToolBar];
    [self createSkuSelectView];
    [self createTableView];
}
// 创建工具栏
- (void)createToolBar {
    
    self.toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    self.toolBar.bottom = SCREEN_HEIGHT - 64;
    self.toolBar.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self.view addSubview:self.toolBar];
    
    // 去查看购物车的视图
    UIControl *gotoView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, self.toolBar.height * 1.5, self.toolBar.height)];
    [self.toolBar addSubview:gotoView];
    [gotoView addTarget:self action:@selector(gotoShopCar) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    imgView.image = [UIImage imageNamed:@"fenlei_chaiyou"];
    [gotoView addSubview:imgView];
    imgView.x = gotoView.width / 2;
    imgView.y = 20 / 2;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, gotoView.width, 20)];
    label.adjustsFontSizeToFitWidth = YES;
    label.font = FONT(12);
    label.text = @"购物车";
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [gotoView addSubview:label];
    
    // 加入购物车按钮
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(self.toolBar.width / 3 * 2, 0, self.toolBar.width / 3, self.toolBar.height);
    addBtn.backgroundColor = HTB_THEME_COLOR;
    addBtn.titleLabel.font = FONT(15);
    [addBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    [self.toolBar addSubview:addBtn];
}
// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.toolBar.height) style:UITableViewStylePlain];
    self.table.backgroundColor = TABLE_COLOR;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    self.table.tableHeaderView = self.carousel;
    self.table.tableFooterView = [UIView new];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)createSkuSelectView {
    
    self.skuView = [[UIControl alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, 40)];
    self.skuView.backgroundColor = HTB_BG_GRAY;
    self.skuView.layer.cornerRadius = 8;
    [self.skuView addTarget:self action:@selector(selectSku) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
    label.adjustsFontSizeToFitWidth = YES;
    label.font = FONT(14);
    label.text = @"选择规格";
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentLeft;
    [self.skuView addSubview:label];
    label.tag = 100;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    imageView.backgroundColor = [UIColor orangeColor];
    [self.skuView addSubview:imageView];
    imageView.right = self.skuView.width - 10;
    imageView.y = self.skuView.height / 2;
    
    // 设置label的宽度
    label.width = imageView.left - 10 - label.left;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 信息
    if (indexPath.row == 0) {
        
        ByShopDetailProductInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
        if (cell == nil) {
            cell = [[ByShopDetailProductInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"infoCell"];
        }
        
        ByShopDetailProductInfoCellLayout *layout = [[ByShopDetailProductInfoCellLayout alloc] init];
        layout.model = self.model;
        cell.layout = layout;
        
        return cell;
    }
    
    // 选规格
    if (indexPath.row == 1) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"skuCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"skuCell"];
        }
    
        [cell.contentView addSubview:self.skuView];
        
        return cell;
    }
    
    // 介绍
    if (indexPath.row == 2) {
        
        ByShopDetailProductDescCell *cell = [tableView dequeueReusableCellWithIdentifier:@"descCell"];
        if (cell == nil) {
            cell = [[ByShopDetailProductDescCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"descCell"];
        }
        
        ByShopDetailProductDescCellLayout *layout = [[ByShopDetailProductDescCellLayout alloc] init];
        layout.model = self.model;
        cell.layout = layout;
        
        return cell;
    }
    
    
    
    
    return [UITableViewCell new];
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ByShopDetailProductInfoCellLayout *layout = [[ByShopDetailProductInfoCellLayout alloc] init];
        layout.model = self.model;
        return layout.cellFrame.size.height;
    }
    if (indexPath.row == 1) {
        return 40;
    }
    if (indexPath.row == 2) {
        ByShopDetailProductDescCellLayout *layout = [[ByShopDetailProductDescCellLayout alloc] init];
        layout.model = self.model;
        return layout.cellFrame.size.height;
    }
    return 44;
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
  
}


#pragma mark - 点击事件
- (void)selectSku {
    
    ByShopProductDetailSkuVC *vc = [[ByShopProductDetailSkuVC alloc] init];
    vc.detailModel = self.model;
    vc.backAction = ^ (SkuModel *model, NSInteger count) {
        UILabel *label = [self.skuView viewWithTag:100];
        label.text = [NSString stringWithFormat:@"%@    x %li", model.sku_desc, count];
    };
    [self xw_presentViewController:vc withAnimator:self.bottomPopAnimator];
}
- (void)gotoShopCar {
    
}
- (void)add {
    
    
}

@end
